import sys, types

def isAuthorized(ch,command):
  "Return true if ch can call command, false otherwise"
  # See if the command has any roles assigned to it
  permTable = ch.d.server.permTable
  if command in permTable and len(permTable[command]) > 0:
    if not hasattr(ch,"roles"): # if the player has no roles, they can't use the command
      return False
    else:
      for r in permTable[command]:
        if r in ch.roles:
          return True
      return False
  else:
    return True # command has no roles assigned, anyone can use it

def role_command(ch, args):
  """
  Usage:
  role set <command or player> [role]
  role add [rolename]
  role list [command or player]
  
  Sets a role to a command or player.
  Adds a role to the availble role list.
  Displays assigned roles on commands or players.
  """
  try:
    opt = args[0]
  except:
    ch.interpret(ch,"help role")
    return
    
  # SET A ROLE
  if opt == "set":  
    try:
      targ = args[1]
    except:
      ch.toSelf("You must supply a target")
      return
    
    try:
      role = args[2]
    except:
      ch.toSelf("Add which role?")
      return
    
    # ensure it's a good role
    if role not in ch.d.server.roles:
      ch.toSelf("Couldn't find the role: %s" % role)
      return
    
    # find the actual target and assign the role
    if targ in ch.d.server.commandTable:
      if targ in ch.d.server.permTable:
        ch.d.server.permTable[targ].append(role)
      else:
        ch.d.server.permTable[targ] = [role]
      ch.toSelf("Added %s to %s." % (role,targ) )
      ch.d.server.savePerms()
    elif targ in ch.d.server.connections:
      target = ch.d.server.connections[targ].character
      if type(target.roles) is types.NoneType:
        target.roles = [role]
      else:
        target.roles.append(role)
      target.d.save()
      ch.toSelf("Added %s to %s." % (role,targ) )
    else:
      ch.toSelf("Couldn't find %s." % targ)
      return
      
  # ADD A NEW ROLE    
  elif opt == "add":
    try:
      r = args[1]
      if r not in ch.d.server.roles:
        ch.d.server.roles.append(r)
        ch.d.server.saveRoles()
        ch.toSelf("Added %s to available roles." % r)
      else:
        ch.toSelf("%s is already an available role." % r)
    except:
      ch.toSelf("Add what role?")
      return
      
  elif opt == "list":
    # try to list roles of an object
    try:
      targ = args[1]
      if targ in ch.d.server.commandTable:
        if targ in ch.d.server.permTable:
          ch.toSelf( "Roles for %s:\n%s" % (targ, ", ".join(ch.d.server.permTable[targ]) ) )
          return
        else:
          ch.toSelf( "%s has no assigned roles." % targ.capitalize() )
          return
      else:
        target = ch.getPlayerInWorld(targ)
        if target is not None:
          try:
            r = getattr(ch.d.server.connections[targ].character,"roles")
            if len(r) > 0:
              ch.toSelf( "Roles for %s:\n%s" % (targ,", ".join(r)) )
              return
            else:
              ch.toSelf( "%s has no assigned roles." % targ )
          except:
            print sys.exc_info()
            print sys.exc_info()[0]
            ch.toSelf( "Could not access roles for %s" % targ )
            return
        else:
          ch.toSelf( "Could not find %s" % targ )
          return
    # List the roles on the server
    except:
      ch.toSelf( "Roles defined on the server:\n%s" % "\n".join(ch.d.server.roles) )
    return
  else:
    ch.interpret("help role")
    return
  
